﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc.Html;
using System.Web.Mvc;
using System.Linq.Expressions;
using BattleThis.Core;
using System.Drawing;
using System.Web.Script.Serialization;

namespace BattleThis.Helpers
{

	public static class HtmlHelperExtension
	{
		/// <summary>
		/// Renders the widgets.
		/// </summary>
		/// <param name="html">The HTML.</param>
		public static MvcHtmlString Widgets( this HtmlHelper html )
		{
			return html.Action( BattleThis.Controllers.WidgetController.IndexAction,
								BattleThis.Controllers.WidgetController.Info.ControllerName );
		}

		/// <summary>
		/// Overlays for.
		/// </summary>
		/// <typeparam name="TModel">The type of the model.</typeparam>
		/// <param name="html">The HTML.</param>
		/// <param name="expression">The expression.</param>
		/// <returns></returns>
		public static MvcHtmlString H1For<TModel>( this HtmlHelper<TModel> html, Expression<Func<TModel, Game>> expression )
		{
			var model=	ModelMetadata.FromLambdaExpression( expression, html.ViewData );
			var game = (Game)model.Model;
			var context = html.ViewContext.HttpContext.GetGameContext();
			var text = string.Format("{0} on Map {1} in Game ({2})", game.CurrentPlayer.Name, game.Map.Name,game.Name );
			var tag = new TagBuilder("h1");

			tag.SetInnerText(text);

			return MvcHtmlString.Create( tag.ToString());
		}

		/// <summary>
		/// Overlays for.
		/// </summary>
		/// <typeparam name="TModel">The type of the model.</typeparam>
		/// <param name="html">The HTML.</param>
		/// <param name="expression">The expression.</param>
		/// <returns></returns>
		public static MvcHtmlString OverlayFor<TModel>( this HtmlHelper<TModel> html, Expression<Func<TModel, Field>> expression )
		{
			var model=	ModelMetadata.FromLambdaExpression( expression, html.ViewData );
			var field = (Field)model.Model;
			var context = html.ViewContext.HttpContext.GetGameContext();
			var tagBuilder = new TagBuilder("img");
			var cssName = "overlay";
			var urlHelper = new UrlHelper( html.ViewContext.RequestContext );

			if( context.Selection.IsNone() ) return MvcHtmlString.Empty;

			if( !field.Unit.IsEmpty() && context.Selection.IsInAttackRange( field ) )
			{
				tagBuilder.AddCssClass( cssName );
				tagBuilder.Attributes.Add( "alt", "NoAttackable" );
				tagBuilder.Attributes.Add( "src", urlHelper.SrcFor( "OverlayRed" ) );
			}
			else if(!context.Selection.IsInMoveRange(field))
			{
				tagBuilder.AddCssClass( cssName );
				tagBuilder.Attributes.Add( "alt", "Inaccessible" );
				tagBuilder.Attributes.Add( "src", urlHelper.SrcFor( "OverlayGray" ) );
			}
			else
			{
				return MvcHtmlString.Empty;
			}

			return MvcHtmlString.Create(tagBuilder.ToString());
		}

		/// <summary>
		/// Widthes for.
		/// </summary>
		/// <typeparam name="TModel">The type of the model.</typeparam>
		/// <param name="html">The HTML.</param>
		/// <param name="expression">The expression.</param>
		/// <returns></returns>
		public static MvcHtmlString WidthFor<TModel>( this HtmlHelper<TModel> html, Expression<Func<TModel,Map>> expression )
		{
			var modelMetaData =	ModelMetadata.FromLambdaExpression( expression, html.ViewData );
			var map = (Map) modelMetaData.Model;

			return GetInPixel( Definitions.FieldSize.Width * ( map.Size.Columns) - (map.Size.Columns * Definitions.FieldOffset) );
		}

		/// <summary>
		/// Heights for.
		/// </summary>
		/// <typeparam name="TModel">The type of the model.</typeparam>
		/// <param name="html">The HTML.</param>
		/// <param name="expression">The expression.</param>
		/// <returns></returns>
		public static MvcHtmlString HeightFor<TModel>( this HtmlHelper<TModel> html, Expression<Func<TModel, Map>> expression )
		{
			var modelMetaData =	ModelMetadata.FromLambdaExpression( expression, html.ViewData );
			var map = (Map)modelMetaData.Model;

			return GetInPixel( Definitions.FieldSize.Height * ( map.Size.Rows + 0.5f ) );
		}

		/// <summary>
		/// Gets the in pixel.
		/// </summary>
		/// <param name="value">The value.</param>
		/// <returns></returns>
		private static MvcHtmlString GetInPixel( float value )
		{
			return MvcHtmlString.Create(string.Format("{0}px", value ));
		}

        public static IHtmlString ToJson(this HtmlHelper html, object toConvert)
        {
           JavaScriptSerializer oSerializer = new JavaScriptSerializer();
           return html.Raw( oSerializer.Serialize(toConvert));
        }

	}
}